package com.wwwc.admin.web.servlet;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import java.sql.SQLException;

import com.wwwc.admin.web.database.*;
import com.wwwc.util.web.*;

public class adminProductManager extends HttpServlet {
    public void doPost(HttpServletRequest request, HttpServletResponse response) {
	
	try {
	    response.setContentType("text/html");

	    String user_ip       = request.getRemoteAddr();
	    HttpSession session  = request.getSession(true);
	    PrintWriter out      = response.getWriter();

	    ResourceBundle config = (ResourceBundle)session.getAttribute("Configuration");
	
	    if (config == null) {
		return;
	    }

	    String databases   = config.getString("3wcenter.databases");

	    if(config == null) {
		System.out.println("adminProductManager:Error:101");
		return;
	    }

	    if(databases == null || databases.length()<1) {
		System.out.println("adminProductManager:Error:102");
		return;
	    }

	    ResourceBundle adminInfo = (ResourceBundle)session.getAttribute("adminInfo");
	    if (adminInfo == null) {
		Locale locale = request.getLocale();
		adminInfo = ResourceBundle.getBundle("admin", locale); 
		session.setAttribute("adminInfo", adminInfo);
	    }
	    
	    if(adminInfo == null) {
		System.out.println("adminProductManager:Error:103");
		return;
	    }

	    String admin_ips    = adminInfo.getString("admin.ips");
	    if(user_ip == null || !user_ip.startsWith(admin_ips)) {
		System.out.println("adminProductManager:Error:104");
		return;
	    }

	    String action = request.getParameter("aid");
	    String call_path = request.getContextPath()+request.getServletPath();
	    String who = request.getParameter("who");
	    if(who == null) {
		System.out.println("adminProductManager:Error:106");
		return;
	    }

	    adminProductManagerBean pBean = new adminProductManagerBean();

	    //===================================================================
	    // default page
	    //===================================================================
	    String add_call_back    = "1";
	    String update_call_back = "2";
	    String delete_call_back = "3";

	    String database = request.getParameter("database");
	    out.println(pBean.getDatabaseOptions(config, call_path, database));
	    if(database == null) {
		return;
	    }

	    if(who.equals("m")) {
		out.println(pBean.managerBar(call_path, add_call_back, update_call_back, delete_call_back, action, database));
	    }

	    Database db = DatabaseAdapter.getDatabase(database, config);
	    
	    if(who.equals("m") && action != null && action.equals(add_call_back)) {
		out.println(pBean.addProduct(config, request, call_path, add_call_back, database));
	    }
	    /*
	    if(who.equals("m") && action != null && action.equals(storage_payment_cb)) {
		out.println(uaBean.processStoragePayment(request, call_path, storage_payment_cb));
	    }

	    if(who.equals("m") && action != null && action.equals(storage_call_back)) {
		out.println(uaBean.addUserStorage(request, call_path, storage_call_back));
	    }

	    if(who.equals("l")) {
		out.println("Account<BR>");
		out.println("Storage<BR>");
	    }
	    */
	}
	catch(Exception e) {
	    System.out.println("adminProductManager:Error:401"+e);
	}
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response)
	throws IOException, ServletException 
    {
	doPost(request, response);
    }
}








